package View;

import home.Config;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;


public class Home extends JPanel implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4092886805638826148L;
	private JPanel header = new JPanel(new FlowLayout(FlowLayout.LEFT));
	private JPanel content = new JPanel();
	private JPanel sidebar = new JPanel(new GridBagLayout());

	public Home(){
		
		super(new GridBagLayout());
		
		GridBagConstraints con = new GridBagConstraints();
		
		this.setHeader(header);
		con.fill = GridBagConstraints.BOTH;
		con.weightx = 0.5;
		con.weighty = 0;
		con.gridwidth = 4;
		con.gridx = 0;
		con.gridy = 0;
		this.add(header, con); 
		
		this.setcontent(content, "Ciao");
		con.fill = GridBagConstraints.BOTH;
		con.weightx = 0.5;
		con.weighty = 0.5;
		con.gridx = 0;
		con.gridy = 1;
		con.gridwidth = 3;
		this.add(content, con);

		this.setSidebar(sidebar, "Preferiti");
		con.fill = GridBagConstraints.BOTH;
		con.weightx = 0.165;
		con.weighty = 0.5;
		con.gridx = 3;
		con.gridy = 1;
		con.gridwidth = 1;
		this.add(sidebar, con);
       
	}
	
	private JPanel setSidebar(JPanel panel, String tit){
		
		// custumizzo la sideabar
		panel.setMinimumSize(new Dimension(240,500));
		panel.setPreferredSize(new Dimension(240,500));
		
		// creo il contraints
		GridBagConstraints c = new GridBagConstraints();
		
		//crea il titolo della tabella e lo aggiunge al panel
		JPanel titolo = new JPanel();
		titolo.setBackground(Config.getConfig().getRosaChiaro());
		titolo.setBorder(BorderFactory.createLineBorder(Color.white));
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.weighty = 0.1;
		c.gridx = 0;
		c.gridy = 0;
		JLabel lab = new JLabel(tit);
		lab.setFont(Config.getConfig().getFont().deriveFont((float) 14));
		lab.setForeground(Color.white);
		titolo.add(lab);
		panel.add(titolo,c);
		
		// crea gli altri campi e li aggiunge
		for(int i=0; i<5; i++){
			JPanel campo = new JPanel(new FlowLayout(FlowLayout.CENTER));
			campo.setBackground(Config.getConfig().getMarrone());
			campo.setBorder(BorderFactory.createLineBorder(Color.white));
			c.fill = GridBagConstraints.BOTH;
			c.weightx = 0.5;
			c.weighty = 0.5;
			c.gridx = 0;
			c.gridy = i+1;
			JLabel l1 = new JLabel(Clio.createImageIcon("images/video.jpg"));
			l1.setMaximumSize(new Dimension(105, 92));
			l1.setPreferredSize(new Dimension(105, 92));
			JLabel l2 = new JLabel("<HTML>Ricerca jjj jjj lkjpp<br> jjj hhh jjj jjjj jjjj jjjj jjj fuffo Avanzata fuffo fuffo fuffo fuffo fuffo fuuuu iijhn fuffo</HTML>");
			l2.setPreferredSize(new Dimension(120, 90));
			l2.setMinimumSize(new Dimension(120, 90));
			campo.add(l1);
			campo.add(l2);
			panel.add(campo,c);
		}
		return panel;
		
	}
	

	private JPanel setcontent(JPanel panel, String tit){
		
		// custumizza il content
		panel.setBackground(Config.getConfig().getRosa());
		panel.setMinimumSize(new Dimension(720,500));
		panel.setPreferredSize(new Dimension(720,500));
		panel.setBorder(BorderFactory.createLineBorder(Color.white));
		
		// crea il panel contente le immagini e lo aggiunge	
	    //JLabel immagine = new JLabel(Clio.createImageIcon("images/sfondo.png"));
	    //panel.add(immagine);
	    
	    // bottone a sbuffo
	    JButton b = new JButton("bottone a sbuffo");
	    b.addActionListener(this);
	    panel.add(b);
	    
		return panel;
		
	}
	
	private JPanel setHeader(JPanel panel){
				
		// custumizza l'header
		panel.setBackground(Config.getConfig().getMarrone());
		panel.setBorder(BorderFactory.createLineBorder(Color.white));
		panel.setPreferredSize(new Dimension(966, 30));
		panel.setMinimumSize(new Dimension(966, 30));
		
		// crea il primo bottone e lo aggiunge
	    JButton x = new JButton();
	    x.setFont(Config.getConfig().getFont().deriveFont((float)15));
		x.setForeground(Color.white);
		x.setText("Home");
		x.setFocusPainted(false);
		x.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,new Color(165,64,66),new Color(204,108,110),null,new Color(108,55,56)));
		x.setContentAreaFilled(true);
	    x.setBackground(Config.getConfig().getCaramello());
	    x.setPreferredSize(new Dimension(100, 18));
	    panel.add(x);
	    
		return panel;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		Ricerca ric = new Ricerca();
		Clio.AggiornaGUI(ric, "ricerca");
		
	}

	

}
